﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using ERP.Domain.Processs;
using MediatR;
using Sell.Api.Read.Application.Command.ProcesssCommand;
using SqlSugar;

namespace Sell.Api.Read.Application.CommandHandlers.ProcesssCommandHandlers
{
    public class processtypelistCommandHandlers : IRequestHandler<processtypelistCommand, ApiResult<ApiPaging<ProcessType>>>
    {
        public ISqlSugarClient SqlSugar { get; }
        public processtypelistCommandHandlers(ISqlSugarClient sqlSugar)
        {
            SqlSugar = sqlSugar;
        }
        /// <summary>
        /// 工序分类列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<ApiPaging<ProcessType>>> Handle(processtypelistCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<ProcessType>> result = new ApiResult<ApiPaging<ProcessType>>();
            result.Code = EnumCode.Ok;
            result.Message = "工序分类显示成功";

            // 只查询IsDelete为false的记录
            var query = SqlSugar.Queryable<ProcessType>().Where(x => x.IsDelete == false);
            var list = query.ToList();
            int totalcount = list.Count();
            int totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.pagesize);
            list = list.OrderBy(x => x.Id).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
            ApiPaging<ProcessType> paging = new ApiPaging<ProcessType>();
            paging.TotalCount = totalcount;
            paging.TotalPage = totalpage;
            paging.Data = list.ToList();
            result.Data = paging;
            return Task.FromResult(result);
        }
    }
}
